<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\LakalaModel;
use think\Config;
use think\Db;
use think\Exception;
use app\index\controller\Leshua;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }


    /**
     * 第一步
     * @return mixed
     */
    public function new_zhifu(){
        //查询
        $data = input('get.');

        $data['type'] = $data['type'] ?? '';
        $data['id'] = str_replace('%','%25',$data['id']);
        $data['id'] = str_replace('+','%2B',$data['id']);
        $data['id'] = str_replace(' ','%20',$data['id']);
        $data['id'] = str_replace('/','%2F',$data['id']);
        $data['id'] = str_replace('?','%3F',$data['id']);
        $data['id'] = str_replace('#','%23',$data['id']);
        $data['id'] = str_replace('&','%26',$data['id']);
        $data['id'] = str_replace('=','%3D',$data['id']);
        $this->assign('data',$data);
        $data['id'] = str_replace(' ', '+', $_GET['id']);  // 将空格替换回 +
        $data['time'] = str_replace(' ', '+', $_GET['time']);  // 将空格替换回 +
        if(isset($data['time'])){
            $time = openssl_decrypt(base64_decode($data['time']), 'AES-128-ECB','ming', OPENSSL_RAW_DATA);
            if($time == date('Y-m-d')){
                $this->assign('time',1);
            }else{
                $this->assign('time',2);
            }
        }else{
            $this->assign('time',2);
        }
        $data['id'] = openssl_decrypt(base64_decode($data['id']), 'AES-128-ECB','zhifubao', OPENSSL_RAW_DATA);
        $res =  Db::name('list_one')->where('id',$data['id'])->find();
        $sign = Db::name('sign')->where('orderid',$data['id'])->find();
        $this->assign('res',$res);
        $this->view->engine->layout(false);
        $this->assign('sign',$sign);
        //判断支付成功了吗
        if($res['jiaofei'] == 2){
            //查询对应的投统项目
            $xiangmu = '';
            if($res['main_name'] == 1){
                $xiangmu .= '机动车损失服务、';
            }
            if($res['main_disan'] == 1){
                $xiangmu .= '机动车第三者责任服务、';
            }
            if($res['siji'] == 1){
                $xiangmu .= '车上人员责任服务(司机) 、';
            }
            if($res['chengke'] == 1){
                $xiangmu .= '车上人员责任服务(乘客)、';
            }
            if($res['dao_name'] == 1){
                $xiangmu .= '医保外用药、';
            }
            if($res['dianwang'] == 1){
                $xiangmu .= '外部电网故障损失险、';
            }
            if($xiangmu){
                $xiangmu = substr($xiangmu,0,strlen($xiangmu)-3);
            }
            $this->assign('xiangmu',$xiangmu);
            return $this->fetch('new_zhifu_end');
        }
        return $this->fetch('new_zhifu');
    }

    /**
     * 电子单
     * @return mixed|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function dian()
    {
        $data = input('get.');
        $data['id'] = str_replace('%','%25',$data['id']);
        $data['id'] = str_replace('+','%2B',$data['id']);
        $data['id'] = str_replace(' ','%20',$data['id']);
        $data['id'] = str_replace('/','%2F',$data['id']);
        $data['id'] = str_replace('?','%3F',$data['id']);
        $data['id'] = str_replace('#','%23',$data['id']);
        $data['id'] = str_replace('&','%26',$data['id']);
        $data['id'] = str_replace('=','%3D',$data['id']);
        $this->assign('data',$data);
        $id = $_GET['dian'];
        $data['id'] = str_replace(' ', '+', $_GET['id']);  // 将空格替换回 +
        $data['time'] = str_replace(' ', '+', $_GET['time']);  // 将空格替换回 +
        $data['id'] = openssl_decrypt(base64_decode($data['id']), 'AES-128-ECB','zhifubao', OPENSSL_RAW_DATA);
//        dump($data);die;
        $this->assign('orderid',$id);
        if(empty($id)){
            $this->error('请先选择要生成服务单的对象');
        }else{
            $res = Db::name('list_one')->where('id',$id)->find();
            $res['car_use_nature_str'] = $this->car_use_nature_str($res['car_use_nature']);
            $tc_list = array();
            $k=0;
            $line = 8;
            if($res['main_standard']){
                $tc_list[$k]['tczl']='机动车车损损失';
                $tc_list[$k]['bjmp'] = $res['main_franchise'];
                $tc_list[$k]['money']= $res['main_money'];
                $tc_list[$k]['tcf']= $res['main_submit'];
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = round(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['main_standard'],2),100,4),2);
//                }
                $k=$k+1;
            }
            if($res['main_biao']){
                $tc_list[$k]['tczl']='机动车第三者责任';
                $tc_list[$k]['bjmp'] = $res['main_ji'];
                $tc_list[$k]['money']= $res['main_santong'];
                $tc_list[$k]['tcf']= $res['main_yingjiao'];
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = round(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['main_biao'],2),100,4),2);
//                }
                $k=$k+1;
            }
            if($res['dao_biao']){
                $tc_list[$k]['tczl']='医保外用药';
                $tc_list[$k]['bjmp'] = $res['dao_ji'];
                $tc_list[$k]['money']= $res['dao_tong'];
                $tc_list[$k]['tcf']= $res['dao_ying'];
                $k=$k+1;
            }
            if($res['siji_biao']){
                $tc_list[$k]['tczl']='机动车车上人员-司机 *1';
                $tc_list[$k]['bjmp'] = $res['siji_ji'];
                $tc_list[$k]['money']= $res['siji_tong'];
                $tc_list[$k]['tcf']= $res['siji_ying'];
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = round(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['siji_biao'],2),100,4),2);
//                }
                $k=$k+1;
            }
            if($res['ck_biao']){
                $tc_list[$k]['tczl']='机动车车上人员-乘客 *' . (string)($res['car_carry_peo'] - 1);
                $tc_list[$k]['bjmp'] = $res['ck_ji'];
                $tc_list[$k]['money']= $res['ck_tong'];
                $tc_list[$k]['tcf']= $res['ck_ying'];
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = round(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['ck_biao'],2),100,4),2);
//                }
                $k=$k+1;
            }
            if($res['dw_biao']){
                $tc_list[$k]['tczl']='外部电网故障损失险';
                $tc_list[$k]['bjmp'] = $res['dw_ji'];
                $tc_list[$k]['money']= $res['dw_tong'];
                $tc_list[$k]['tcf']= $res['dw_ying'];
                $k=$k+1;
            }
            $line = $line - $k;
            $this->assign('tc_list',$tc_list);
            $this->assign('line',$line);
            $tiaokuan = db('tiaokuan')->where('gs_type',1)->order('id desc')->limit(1)->find();
            $this->assign('tiaokuan',$tiaokuan);
            $this->assign('res',$res);
            $sign = Db::name('sign')->where('orderid',$id)->find();
            $this->assign('sign',$sign);
            // 临时关闭当前模板的布局功能
            $this->view->engine->layout(false);

            return $this->fetch();

        }
    }
    function car_use_nature_str($value) {
        try {
            return model('Constants')->CAR_USE_NATURE[$value];
        } catch (Exception $e) {
            return '';
        }
    }

    /**
     * 支付页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function zhifu(){
        //查询
        $data = input('get.');
        $data['id'] = str_replace(' ', '+', $_GET['id']);  // 将空格替换回 +
        $data['time'] = str_replace(' ', '+', $_GET['time']);  // 将空格替换回 +
        if(isset($data['time'])){
            $time = openssl_decrypt(base64_decode($data['time']), 'AES-128-ECB','ming', OPENSSL_RAW_DATA);
            //  dump($time);die;
            if($time == date('Y-m-d')){
                $this->assign('time',1);
            }else{
                $this->assign('time',2);
            }
        }else{
            $this->assign('time',2);
        }
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false){
            //判断是不是微信
            // return "您正在使用 微信 扫码";
            $data['id'] = openssl_decrypt(base64_decode($data['id']), 'AES-128-ECB','zhifubao', OPENSSL_RAW_DATA);
            $res =  Db::name('list_one')->where('id',$data['id'])->find();

            if(isset($data['type'])){
                $data['type'] = openssl_decrypt(base64_decode($data['type']), 'AES-128-ECB',$data['id'], OPENSSL_RAW_DATA);
                if($data['type'] == 1){
                    $res['type'] = 2;
                    $res['main_free'] = bcmul(((100-$res['feiyong'])/100),$res['main_free'],2);
                }
            }else{
                // if($res['kehu_sumdiscount']){
                //     $res['main_sumdiscount'] = $res['kehu_sumdiscount'];
                //     $main_sum = $res['main_sum'];
                //     if($res['dao_name'] == 1){
                //         // $main_sum = bcsub($res['main_sum'],$res['dao_ying'],2);
                //     }
                //     $kehu_tongchou = round(bcdiv(bcmul($res['kehu_sumdiscount'],$main_sum,2),100,4),2);

                //     if($res['dao_name'] == 1){
                //         // $kehu_tongchou = bcadd($kehu_tongchou,$res['dao_ying'],2);
                //     }

                //     // $res['main_free'] = $kehu_tongchou;
                //     // 			$res['main_free'] = round(bcdiv(bcmul($res['kehu_sumdiscount'],$res['main_sum'],2),100,4),2);
                // }
            }
            //查询对应的投统项目
            $xiangmu = '';
            if($res['main_name'] == 1){
                $xiangmu .= '机动车损失服务、';
            }
            if($res['main_disan'] == 1){
                $xiangmu .= '机动车第三者责任服务、';
            }
            if($res['siji'] == 1){
                $xiangmu .= '车上人员责任服务(司机) 、';
            }
            if($res['chengke'] == 1){
                $xiangmu .= '车上人员责任服务(乘客)、';
            }
            if($res['dao_name'] == 1){
                $xiangmu .= '医保外用药	、';
            }
            if($res['dianwang'] == 1){
                $xiangmu .= '外部电网故障损失险、';
            }
            if($xiangmu){
                $xiangmu = substr($xiangmu,0,strlen($xiangmu)-3);
            }
            $pay = Db::name('pay_channel')->alias('a')->join('pay_type b','a.type_id = b.id')->where('a.id',$data['channel_id'])->field('a.*,b.name as type_name')->find();
            if($res['jiaofei'] != 2){
                if($pay['type_name'] == 'leshua'){
                    $model = new Leshua();
                    $jsApiParameters = $model->wx($res,$pay);
                    $this->assign('jsApiParameters',$jsApiParameters);
                }elseif($pay['type_name'] == 'fuyou'){
                    $model = new Fuyou();
                    $jsApiParameters = $model->wx($res,$pay);
                    $this->assign('jsApiParameters',$jsApiParameters);
                }elseif($pay['type_name'] == 'lakala'){
                    $lakala=new LakalaModel();
                    $lakalaReturn=$lakala->pay($res,$pay);
                    if ($lakalaReturn['code']==1){
                        $res['url']=$lakalaReturn['data'];
                    }else{
                        $this->error('错误');
                    }
                }elseif($pay['type_name'] == 'yunzhuo'){
                    $model = new Yunzhuo();
                    $jsApiParameters = $model->wx($res,$pay);
                    $this->assign('jsApiParameters',$jsApiParameters);
                }
            }
            $this->assign('res',$res);
            $this->assign('xiangmu',$xiangmu);
            $this->view->engine->layout(false);
            return $this->fetch($pay['type_name'].'/wx');
        }else{
            //判断是不是支付宝
            $res = $this->zfb($data);
            //查询对应的投统项目
            $xiangmu = '';
            if($res['main_name'] == 1){
                $xiangmu .= '机动车损失服务、';
            }
            if($res['main_disan'] == 1){
                $xiangmu .= '机动车第三者责任服务、';
            }
            if($res['siji'] == 1){
                $xiangmu .= '车上人员责任服务(司机) 、';
            }
            if($res['chengke'] == 1){
                $xiangmu .= '车上人员责任服务(乘客)、';
            }
            if($res['dao_name'] == 1){
                $xiangmu .= '医保外用药	、';
            }
            if($res['dianwang'] == 1){
                $xiangmu .= '外部电网故障损失险、';
            }
            if($xiangmu){
                $xiangmu = substr($xiangmu,0,strlen($xiangmu)-3);
            }
            $this->assign('res',$res);
            $this->assign('xiangmu',$xiangmu);
            $this->view->engine->layout(false);
            return $this->fetch('zhifubao');
        }
    }

    /**
     * 签字查询
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lizi()
    {
        $data = input('get.');
        $data['id'] = str_replace(' ', '+', $_GET['id']);  // 将空格替换回 +
        $data['time'] = str_replace(' ', '+', $_GET['time']);  // 将空格替换回 +
        $this->assign('data',$data);
        $data['id'] = openssl_decrypt(base64_decode($data['id']), 'AES-128-ECB','zhifubao', OPENSSL_RAW_DATA);
        $res =  Db::name('list_one')->where('id',$data['id'])->find();
        $sign = Db::name('sign')->where('orderid',$data['id'])->find();

        $this->assign('res',$res);
        $this->assign('orderid',$data['id']);
        $this->view->engine->layout(false);

        if($res['jiaofei'] == 1 && $sign){
            return $this->dian2($res['id']);
        }elseif($res['jiaofei'] == 2){
            //查询对应的投统项目
            $xiangmu = '';
            if($res['main_name'] == 1){
                $xiangmu .= '机动车损失服务、';
            }
            if($res['main_disan'] == 1){
                $xiangmu .= '机动车第三者责任服务、';
            }
            if($res['siji'] == 1){
                $xiangmu .= '车上人员责任服务(司机) 、';
            }
            if($res['chengke'] == 1){
                $xiangmu .= '车上人员责任服务(乘客)、';
            }
            if($res['dao_name'] == 1){
                $xiangmu .= '医保外用药	、';
            }
            if($res['dianwang'] == 1){
                $xiangmu .= '外部电网故障损失险、';
            }
            if($xiangmu){
                $xiangmu = substr($xiangmu,0,strlen($xiangmu)-3);
            }
            $this->assign('xiangmu',$xiangmu);
            return $this->fetch('new_zhifu_end');
        }
        return $this->fetch();
    }

    public function xiazai()
    {

        $id = $_GET['dian'];

        $res = Db::name('list_one')->where(['id'=>$id])->find();
        $file = $res['pdf']; // 要下载的文件路径
        $file = ltrim($file, "/");
        // dump(file_exists($file));die;
        $filename = $res['car_num'].'.pdf'; // 下载时的文件名
        // 检查文件是否存在
        if (file_exists($file)) {
            // 设置响应头信息
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="' . $filename . '"');
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));

            // 逐块读取并输出文件内容
            $chunkSize = 8192; // 每次读取的块大小
            $fileHandle = fopen($file, 'rb'); // 以二进制只读模式打开文件

            while (!feof($fileHandle)) {
                echo fread($fileHandle, $chunkSize);
                flush();
            }

            fclose($fileHandle);
            exit;
        } else {
            die('文件不存在');
        }
    }

    /**
     * 生成签名图片
     * @return \think\response\Json
     */
    public function imgbas()
    {
        $image = input('post.file');
        $imageName = "25220_".date("His",time())."_".rand(1111,9999).'.png';
        if (strstr($image,",")){
            $image = explode(',',$image);
            $image = $image[1];
        }
        $path = "sign/".date("Ymd",time());
        if (!is_dir($path)){ //判断目录是否存在 不存在就创建
            mkdir($path,0777,true);
        }
        $imageSrc= $path."/". $imageName; //图片名字
        $r = file_put_contents($imageSrc, base64_decode($image));//返回的是字节数
        if (!$r) {
            $tmparr1=array('data'=>null,"status"=>500,"msg"=>"图片生成失败");
            return json($tmparr1);
        }else{
            $tmparr2=array('data'=>$imageSrc,"status"=>200,"msg"=>"图片生成成功");
            return json($tmparr2);
        }
    }

    /**
     * 保存签名
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function postsign()
    {
        $params = request()->param();
        $data = array(
            'orderid'=>$params['id'],
            'sign_pic'=>$params['urls'],
            'sign_time'=>date('Y-m-d H:i:s')
        );
        $issign = Db::name('sign')->where('orderid',$params['id'])->find();
        if($issign){
            return json(['status'=>200,'msg'=>'你已经签字了']);
        }
        $res = Db::name('sign')->insert($data);
        if($res){
            return json(['status'=>200,'msg'=>'签字成功']);
        }else{
            return json(['status'=>500,'msg'=>'签字失败']);
        }
    }

    public  function dian2($id)
    {
        $this->assign('orderid',$id);
        if(empty($id)){
            $this->error('请先选择要生成服务单的对象');
        }else{
            $res = Db::name('list_one')->where('id',$id)->find();
            // if($res['kehu_sumdiscount']){
            //     $res['main_sumdiscount'] = $res['kehu_sumdiscount'];
            // }
            $res['car_use_nature_str'] = $this->car_use_nature_str($res['car_use_nature']);
            $tc_list = array();
            $k=0;
            $line = 8;
            if($res['main_standard']){
                $tc_list[$k]['tczl']='机动车车损损失';
                $tc_list[$k]['bjmp'] = $res['main_franchise'];
                $tc_list[$k]['money']= $res['main_money'];
                $tc_list[$k]['tcf']= $res['main_submit'];
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = round(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['main_standard'],2),100,4),2);
//                }
                $k=$k+1;
            }
            if($res['main_biao']){
                $tc_list[$k]['tczl']='机动车第三者责任';
                $tc_list[$k]['bjmp'] = $res['main_ji'];
                $tc_list[$k]['money']= $res['main_santong'];
                $tc_list[$k]['tcf']= $res['main_yingjiao'];
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = round(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['main_biao'],2),100,4),2);
//                }
                $k=$k+1;
            }
            if($res['dao_biao']){
                $tc_list[$k]['tczl']='医保外用药';
                $tc_list[$k]['bjmp'] = $res['dao_ji'];
                $tc_list[$k]['money']= $res['dao_tong'];
                $tc_list[$k]['tcf']= $res['dao_ying'];
                $k=$k+1;
            }
            if($res['dw_biao']){
                $tc_list[$k]['tczl']='外部电网故障损失险';
                $tc_list[$k]['bjmp'] = $res['dw_ji'];
                $tc_list[$k]['money']= $res['dw_tong'];
                $tc_list[$k]['tcf']= $res['dw_ying'];
                $k=$k+1;
            }
            if($res['siji_biao']){
                $tc_list[$k]['tczl']='机动车车上人员-司机 *1';
                $tc_list[$k]['bjmp'] = $res['siji_ji'];
                $tc_list[$k]['money']= $res['siji_tong'];
                $tc_list[$k]['tcf']= $res['siji_ying'];
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = round(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['siji_biao'],2),100,4),2);
//                }
                $k=$k+1;
            }
            if($res['ck_biao']){
                $tc_list[$k]['tczl']='机动车车上人员-乘客 *' . (string)($res['car_carry_peo'] - 1);
                $tc_list[$k]['bjmp'] = $res['ck_ji'];
                $tc_list[$k]['money']= $res['ck_tong'];
                $tc_list[$k]['tcf']= $res['ck_ying'];
//                if($res['kehu_sumdiscount']){
//                    $tc_list[$k]['tcf'] = round(bcdiv(bcmul((float)$res['kehu_sumdiscount'],(float)$res['ck_biao'],2),100,4),2);
//                }
                $k=$k+1;
            }
            $line = $line - $k;
            $this->assign('tc_list',$tc_list);
            $this->assign('line',$line);
            $tiaokuan = db('tiaokuan')->where('gs_type',1)->order('id desc')->limit(1)->find();
            $this->assign('tiaokuan',$tiaokuan);
            $this->assign('res',$res);
            $sign = Db::name('sign')->where('orderid',$id)->find();
            $this->assign('sign',$sign);
            // 临时关闭当前模板的布局功能
            $this->view->engine->layout(false);
            return $this->fetch('dian');
        }
    }
    public function num($id,$channel_id)
    {
        $data['did'] = $id;
        $data['shen_time'] = time();
        $pay = Db::name('pay_channel')->where('id',$channel_id)->find();
        $data['shen_peo'] = $pay['name'];
        $dataa['yw_finance_peo'] = $pay['name'];
        $dataa['pay_type'] = 1;
        $dataa['pay_fangshi'] = $pay['name'];
        $dataa['yw_shenhe_time'] = time();
        try {
            $lo = Db::name('list_one')->where('id',$id)->find();
            if(isset($lo['status_type'])){
                if($lo['status_type'] == 1){
                    $dataa['status'] = 5;
                }else{
                    $dataa['status'] = 7;
                }
            }else{
                $dataa['status'] = 5;
            }
        } catch (Exception $e) {}
        $area_code = '0000';
        $res = Db::name('yijian')->insert($data);
        $table = 'list_one';
        $field = 'tong_number';
        $prev = 'HZZX';
        $tong_number = $this->gen_tong_number($table, $field, $prev, $area_code);
        $dataa['tong_number'] = $tong_number;
        $dataa['yw_shenhe_time'] = time();
        Db::name('list_one')->where('id',$id)->update($dataa);
    }
    public function gen_tong_number($table, $field, $prev, $area_code) {
        $tong_number = null;
        $year = date('Ymd', time());
        $next_key = 'next_'.$table.'_'.$year;
        $tmp_number = db('system_config')->field('data')->where('id', $next_key)->find();
        if ($tmp_number == null) {
            $tmp_number = 1;
            db('system_config')->insert(['id'=>$next_key, 'data'=>$tmp_number]);
        } else {
            $tmp_number = intval($tmp_number['data']);
        }
        db('system_config')->where('id',$next_key)->update(['id'=>$next_key, 'data'=>$tmp_number+1]);
        $billWaterNo = str_pad($tmp_number, 7, '0', STR_PAD_LEFT);
        $tong_number =  $prev . $area_code . $year . $billWaterNo;
        return $tong_number;
    }
    /**
     * 上传文件
     */
    public function upload()
    {
        Config::set('default_return_type', 'json');

        //必须还原upload配置,否则分片及cdnurl函数计算错误
        Config::load(APP_PATH . 'extra/upload.php', 'upload');

        $chunkid = $this->request->post("chunkid");
        if ($chunkid) {
            if (!Config::get('upload.chunking')) {
                $this->error(__('Chunk file disabled'));
            }
            $action = $this->request->post("action");
            $chunkindex = $this->request->post("chunkindex/d");
            $chunkcount = $this->request->post("chunkcount/d");
            $filename = $this->request->post("filename");
            $method = $this->request->method(true);
            if ($action == 'merge') {
                $attachment = null;
                //合并分片文件
                try {
                    $upload = new Upload();
                    $attachment = $upload->merge($chunkid, $chunkcount, $filename);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
            } elseif ($method == 'clean') {
                //删除冗余的分片文件
                try {
                    $upload = new Upload();
                    $upload->clean($chunkid);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            } else {
                //上传分片文件
                //默认普通上传文件
                $file = $this->request->file('file');
                try {
                    $upload = new Upload($file);
                    $upload->chunk($chunkid, $chunkindex, $chunkcount);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
        } else {
            $attachment = null;
            //默认普通上传文件
            $file = $this->request->file('file');
            try {
                $upload = new Upload($file);
                $attachment = $upload->upload();
            } catch (UploadException $e) {
                $this->error($e->getMessage());
            }
            $data['status'] = 200;
            $data['data'] = $attachment->url;
            $data['msg'] = '上传成功';
            return json($data);
        }
    }
    //到时间了状态自动改回
    function time_status()
    {
        $one = Db::name('list_one')->whereIn('status',[1,2,3])->select();
        foreach($one as $k => $v){
            $data = array();
            $data['shen_time'] = time();
            $data['shen_peo'] = '系统操作';
            $data['shen_yijian'] = '超时退回修改';
            $data['did'] = $v['id'];
            $res = Db::name('yijian')->insert($data);
            //判断是否有已修改折折扣的
            $list_one_hf = Db::name('list_one_hf')->where(['one_id'=>$v['id']])->find();
            if($list_one_hf){
                $dataa = [
                    'main_sumdiscount'=>$list_one_hf['main_sumdiscount'],
                    'main_money' => $list_one_hf['main_money'],
                    'main_standard' => $list_one_hf['main_standard'],
                    'main_discount' => $list_one_hf['main_discount'],
                    'main_submit' => $list_one_hf['main_submit'],
                    'main_santong' => $list_one_hf['main_santong'],
                    'main_biao' => $list_one_hf['main_biao'],
                    'main_zhe' => $list_one_hf['main_zhe'],
                    'main_yingjiao' => $list_one_hf['main_yingjiao'],
                    'siji_tong' => $list_one_hf['siji_tong'],
                    'siji_biao' => $list_one_hf['siji_biao'],
                    'siji_zhe' => $list_one_hf['siji_zhe'],
                    'siji_ying' => $list_one_hf['siji_ying'],
                    'ck_money' => $list_one_hf['ck_money'],
                    'chengke_zuo' => $list_one_hf['chengke_zuo'],
                    'ck_tong' => $list_one_hf['ck_tong'],
                    'ck_biao' => $list_one_hf['ck_biao'],
                    'ck_zhe' => $list_one_hf['ck_zhe'],
                    'ck_ying' => $list_one_hf['ck_ying'],
                    'dao_tong' => $list_one_hf['dao_tong'],
                    'dao_biao' => $list_one_hf['dao_biao'],
                    'dao_zhe' => $list_one_hf['dao_zhe'],
                    'dao_ying' => $list_one_hf['dao_ying'],
                    'ziran_c' => $list_one_hf['ziran_c'],
                    'zi_tong' => $list_one_hf['zi_tong'],
                    'zi_biao' => $list_one_hf['zi_biao'],
                    'zi_zhe' => $list_one_hf['zi_zhe'],
                    'zi_ying' => $list_one_hf['zi_ying'],
                    'fd_c' => $list_one_hf['fd_c'],
                    'fdj_tong' => $list_one_hf['fdj_tong'],
                    'fdj_biao' => $list_one_hf['fdj_biao'],
                    'fdj_zhe' => $list_one_hf['fdj_zhe'],
                    'fdj_ying' => $list_one_hf['fdj_ying'],
                    'zd_tong' => $list_one_hf['zd_tong'],
                    'zd_biao' => $list_one_hf['zd_biao'],
                    'zd_zhe' => $list_one_hf['zd_zhe'],
                    'zd_ying' => $list_one_hf['zd_ying'],
                    'huowu_c' => $list_one_hf['huowu_c'],
                    'hw_tong' => $list_one_hf['hw_tong'],
                    'hw_biao' => $list_one_hf['hw_biao'],
                    'hw_zhe' => $list_one_hf['hw_zhe'],
                    'hw_ying' => $list_one_hf['hw_ying'],
                    'boli_c' => $list_one_hf['boli_c'],
                    'bl_tong' => $list_one_hf['bl_tong'],
                    'bl_biao' => $list_one_hf['bl_biao'],
                    'bl_zhe' => $list_one_hf['bl_zhe'],
                    'bl_ying' => $list_one_hf['bl_ying'],
                    'bu_tong' => $list_one_hf['bu_tong'],
                    'bu_biao' => $list_one_hf['bu_biao'],
                    'bu_zhe' => $list_one_hf['bu_zhe'],
                    'bu_ying' => $list_one_hf['bu_ying'],
                    'main_sum' => $list_one_hf['main_sum'],
                    'main_free' => $list_one_hf['main_free'],
                    'dw_tong' => $list_one_hf['dw_tong'],
                    'dw_biao' => $list_one_hf['dw_biao'],
                    'dw_zhe' => $list_one_hf['dw_zhe'],
                    'dw_ying' => $list_one_hf['dw_ying'],
                ];
            }
            //修改整体状态
            $dataa['status'] = 4;
            $ress = Db::name('list_one')->where('id',$v['id'])->update($dataa);
        }
        return json(['status'=>1,'message'=>'成功']);

    }
    public function sms(){
        //查询
        $data = input('get.');
        $data['id'] = str_replace(' ', '+', $_GET['id']);  // 将空格替换回 +
        $data['time'] = str_replace(' ', '+', $_GET['time']);  // 将空格替换回 +
        $data['type'] = $data['type'] ?? '';
        $this->assign('data',$data);
        $data['id'] = openssl_decrypt(base64_decode($data['id']), 'AES-128-ECB','zhifubao', OPENSSL_RAW_DATA);
        $res =  Db::name('list_one')->where('id',$data['id'])->find();
        $this->assign('res',$res);
        return $this->fetch();
    }

    public function li(){

        $mianze = db('mianze')->order('id desc')->limit(1)->find();
        $this->assign('mianze', $mianze);
        return $this->fetch();
    }

    public function li2(){

        $tiaokuan = db('tiaokuan')->order('id desc')->limit(1)->find();
        $this->assign('tiaokuan', $tiaokuan);
        return $this->fetch();
    }
}
